package com.novasoftware.novagooglemap.adapter;

import java.util.ArrayList;
import java.util.List;

import com.novasoftware.novagooglemap.R;
import com.novasoftware.novagooglemap.activity.CategoryEdit;
import com.novasoftware.novagooglemap.activity.MainActivity;
import com.novasoftware.novagooglemap.db.entity.NovaCategory;
import com.novasoftware.novagooglemap.framework.util.IoC;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainAdapter extends ArrayAdapter<NovaCategory> {

	private  Activity context;
	
	private final ArrayList<NovaCategory> list;

	public MainAdapter(Activity context, ArrayList<NovaCategory> objects) {
		super(context, R.layout.item_main, objects);
		this.context = context;
		list = objects;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		View rowView = convertView;
		if (rowView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			rowView = inflater.inflate(R.layout.item_main, null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.txtCategory = (TextView) rowView
					.findViewById(R.id.txtCategory);
			viewHolder.txtCaseStudy = (TextView) rowView
					.findViewById(R.id.txtCaseStudy);
			viewHolder.txtImage = (ImageView) rowView
					.findViewById(R.id.txtImage);
			rowView.setTag(viewHolder);
			((Button)rowView.findViewById(R.id.btnDetail)).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Log.i("Beegin", "Beddd");
					Intent intent = new Intent(Intent.ACTION_CALL);
					intent.setClass(context, CategoryEdit.class);
					intent.putExtra("NovaCategory", list.get(position));
					context.startActivity(intent);
				}
			});
		}

		ViewHolder holder = (ViewHolder) rowView.getTag();
		NovaCategory novaCategory = list.get(position);
		holder.txtCategory.setText(novaCategory.Name);
		holder.txtCaseStudy.setText(novaCategory.Name);
		holder.txtImage.setImageResource(novaCategory.ImageId);
		return rowView;
	}

	class ViewHolder {
		TextView txtCategory;
		ImageView txtImage;
		TextView txtCaseStudy;
	}

}
